package trading.algo;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import trading.bo.DBAccessorBO;
import trading.bo.EmailAccessorBO;
import trading.bo.IBAccessorBO;
import trading.dataentry.OptionPositionOrOrder;
import trading.dataentry.StockPositionOrOrder;

public class CancelOrderMasterTask extends TimerTask {

	private IBAccessorBO ibAccessorBO;
	private DBAccessorBO dbAccessorBO;
	private EmailAccessorBO emailAccessorBO;
	
	public CancelOrderMasterTask(IBAccessorBO ibAccessorBO, DBAccessorBO dbAccessorBO, EmailAccessorBO emailAccessorBO) {
		this.ibAccessorBO = ibAccessorBO;
		this.dbAccessorBO = dbAccessorBO;
		this.emailAccessorBO = emailAccessorBO;
	}
	
	@Override
	public void run() {
		Timer timer = new Timer();
		int taskID = 0;
		
		List<StockPositionOrOrder> openStockOrders = this.dbAccessorBO.getOpenNonTrailStockOrder();
		openStockOrders.addAll(this.dbAccessorBO.getOpenTrailOrder());
    	for (int i = 0; i < openStockOrders.size(); i++, taskID += 2) {
    		int orderId = Integer.parseInt(openStockOrders.get(i).getOrderId());
    		CancelOrderTask cancelOrderTask = new CancelOrderTask(this.ibAccessorBO, emailAccessorBO, orderId);
    		timer.schedule(cancelOrderTask, taskID * 1000);
    	}
    	
		List<OptionPositionOrOrder> optionOrders = this.dbAccessorBO.getOpenOptionOrders();
        for (int i = 0; i < optionOrders.size(); i++, taskID += 2) {
        	int orderId = Integer.parseInt(optionOrders.get(i).getOrderId());
        	CancelOrderTask cancelOrderTask = new CancelOrderTask(this.ibAccessorBO, emailAccessorBO, orderId);
        	timer.schedule(cancelOrderTask, taskID * 1000);
        }
	}

}
